﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class MonAnDAO : DataProvider
    {
        public ArrayList GetDSMonAn()
        {
            connect();
            string sql = "select * from MonAn";
            adapter = new SqlDataAdapter(sql, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);
            disconnect();
            return arr;
        }

        public bool KiemTraMonAnTonTai(string tenmon)
        {
            connect();
            string sql = "SELECT * FROM MonAn  Where ten_mon_an=N'"+tenmon+"'";

            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            bool flag = false;
            while (reader.Read())
                flag = true;

            disconnect();
            return flag;
        }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            MonAnDTO maDTO = new MonAnDTO();
            maDTO.MONANID = int.Parse(dt.Rows[i][0].ToString());
            maDTO.TENMONAN = dt.Rows[i][1].ToString();
            return (object)maDTO;
        }
        public ArrayList TimKiem(string tukhoa)
        {
            connect();
            string sql = "select * from MonAn where ten_mon_an like '%" + tukhoa + "%' ";
            adapter = new SqlDataAdapter(sql, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);
            disconnect();
            return arr;
        }
        public void ThemMonAn(string tenmonan)
        {
            connect();
            string sql = "insert into MonAn(ten_mon_an) values(N'" + tenmonan + "')";
            executeNonQuery(sql);
            disconnect();            
        }

        public void XoaMonAn(int maMA)
        {
            connect();
            //string deleteCommand1 = "DELETE FROM ChiTietMonAn WHERE mon_an_id = " + maMA + "";
            //string deleteCommand2 = "DELETE FROM NguyenVatLieu WHERE mon_an_id = " + maMA + "";
            string deleteCommand3 = "DELETE FROM MonAn WHERE mon_an_id = " + maMA + "";
            executeNonQuery(deleteCommand3);
            disconnect();

        }

        public int GetMonAnID()
        {
            connect();
            string sql = "SELECT TOP 1 mon_an_id FROM MonAn  ORDER BY mon_an_id DESC";

            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            int res = 0;
            while (reader.Read())
             res= reader.GetInt32(0);    
            
            disconnect();
            return res;
        }

        public void CapNhatMonAn(MonAnDTO dto)
        {
            connect();
            string sql = "Update MonAn set ten_mon_an='" + dto.TENMONAN + "' Where mon_an_id=" + dto.MONANID;
            executeNonQuery(sql);
            disconnect();
        }
    }
}
